package com.sinozo.impl;

import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import com.sinozo.IEmailService;
import com.sinozo.email.EmailSendDTO;
import com.sinozo.errcode.CommonCode;
import com.sinozo.exception.BaseException;
import com.sinozo.utils.ValidateUtil;
import org.springframework.stereotype.Service;

/**
 * 邮箱
 *
 * @author zhehen.lu
 * @date 2025/11/6 00:51
 */
@Service
public class EmailServiceImpl implements IEmailService {

    @Override
    public void sendEmail(EmailSendDTO emailSendDTO) {
        ValidateUtil.notNull(emailSendDTO, CommonCode.PARMA_NOT_NULL);

        // 创建邮件账户配置
        MailAccount account = new MailAccount();
        account.setHost("smtp.gmail.com");
        account.setPort(587);  // Gmail TLS 端口
        account.setAuth(true);
        account.setFrom("your_email@gmail.com");
        account.setUser("your_email@gmail.com");
        account.setPass("your_app_password"); // 使用 使用应用专用密码

        // 关键配置：启用 TLS
        account.setSslEnable(false);   // 不使用 SSL
        account.setStarttlsEnable(true); // 启用 STARTTLS

        // 构建邮箱内容
        StringBuilder content = new StringBuilder();
        content.append("Game Name：").append(emailSendDTO.getGameName());
        content.append("Game Url：").append(emailSendDTO.getGameUrl());
        content.append("In-App Purchase Share-WW：").append(emailSendDTO.getAppPurchaseShareWw());
        content.append("In-App Purchase Share-US：").append(emailSendDTO.getAppPurchaseShareUs());
        content.append("D30 Rentenation WW：").append(emailSendDTO.getRentenationWw());
        content.append("D30 Rentenation US：").append(emailSendDTO.getRentenationUs());
        content.append("");
        content.append("Contact Details：").append(emailSendDTO.getContactDetails());
        content.append("First Name：").append(emailSendDTO.getFirstName());
        content.append("Email Addres：").append(emailSendDTO.getEmailAddres());

        // 发送邮件
        try {
            MailUtil.send(account, "business@abysscoregames.com", emailSendDTO.getAbysscoreWebsite(), content.toString(), false);
        } catch (Exception e) {
            throw new BaseException(CommonCode.GOOGLE_EMAIL_SENDING_EXCEPTION, e.getMessage());
        }
    }
}
